package com.mobilesales.android.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import android.util.Log;

import com.mobilesales.android.services.Configuration;

public class Settings {

	private static Object sync =  new Object();

	private static final String PROPERTY_FILE_PATH = "/data/data/com.mobilesales.android/app.properties";

	private static final String TOKEN = "token";
	private String token = "";

	private static final String EMPLOYEE_ID = "employeeId";
	private int employeeId = 0;

	private static final String SERVER_URL = "serverUrl";
	private String serverUrl = "";
	
	private static final String C2DM_REG_ID = "c2dmRegId";
	private String c2dmRegistrationId = "";	
	
	private static final String SYNC_ON_START = "syncOnStart";
	private boolean syncOnStart = true;		

	private static final String LOGIN = "login";
	private String login = "";

	private static final String PASSWORD = "password";
	private String password = "";
	
	private static final String LANGUAGE = "lang";
	private String language = "";
	
	private static final String C2DM_SERVER_MAIL = "c2dmServerMail";
	private String c2dmServerMail = "";
	
	private static Settings instance = null;
	private static long lastModified = -1;

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}	
	
	public String getServerUrl() {
		return serverUrl;
	}

	public void setServerUrl(String serverUrl) {
		this.serverUrl = serverUrl;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public int getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}
	
	public String getC2dmRegistrationId() {
		return c2dmRegistrationId;
	}

	public void setC2dmRegistrationId(String c2dmRegistrationId) {
		this.c2dmRegistrationId = c2dmRegistrationId;
	}	
	
	public boolean isSyncOnStart() {
		return syncOnStart;
	}

	public void setSyncOnStart(boolean syncOnStart) {
		this.syncOnStart = syncOnStart;
	}	
	
	public String getC2dmServerMail() {
		return c2dmServerMail;
	}

	public void setC2dmServerMail(String c2dmServerMail) {
		this.c2dmServerMail = c2dmServerMail;
	}	

	public static Settings getSettings() {
		synchronized (sync) {
			File f = new File(PROPERTY_FILE_PATH);
			if (!f.exists()) {
				return null;
			}

			boolean wasModified = (lastModified != f.lastModified());
			if (wasModified || instance == null) {
				lastModified = f.lastModified();
				Properties prop = null;
				try {
					prop = load(PROPERTY_FILE_PATH);
					instance = new Settings();
					instance.setLogin(prop.getProperty(LOGIN));
					instance.setPassword(prop.getProperty(PASSWORD));
					instance.setServerUrl(prop.getProperty(SERVER_URL));
					instance.setEmployeeId(Integer.parseInt(prop.getProperty(EMPLOYEE_ID)));
					instance.setC2dmRegistrationId(prop.getProperty(C2DM_REG_ID));
					instance.setC2dmServerMail(prop.getProperty(C2DM_SERVER_MAIL));
					instance.setToken(prop.getProperty(TOKEN));
					instance.setLanguage(prop.getProperty(LANGUAGE));
					instance.setSyncOnStart(Boolean.parseBoolean(prop.getProperty(SYNC_ON_START)));
				} catch (Exception ex) {
					Log.e(LogFlags.Settings, ex.getMessage(),ex);
					f.delete();
					instance = null;
					return null;
				}
				
			}
			return instance;
		}
	}

	public void save() throws IOException {
		synchronized (sync) {
			Properties prop = new Properties();
			prop.setProperty(LOGIN, this.getLogin());
			prop.setProperty(PASSWORD, this.getPassword());
			prop.setProperty(SERVER_URL, this.getServerUrl());
			prop.setProperty(EMPLOYEE_ID, String.valueOf(this.getEmployeeId()));
			prop.setProperty(TOKEN, this.getToken());
			prop.setProperty(C2DM_REG_ID, this.getC2dmRegistrationId());
			prop.setProperty(C2DM_SERVER_MAIL, this.getC2dmServerMail());
			prop.setProperty(SYNC_ON_START, String.valueOf(this.isSyncOnStart()));
			prop.setProperty(LANGUAGE, this.getLanguage());
			this.create(prop, PROPERTY_FILE_PATH);
		}
	}

	private static Properties load(String filename)
			throws FileNotFoundException {
		File f = new File(filename);
		if (!f.exists()) {
			throw new FileNotFoundException();
		}
		Properties cache = new Properties();
		FileInputStream in;
		try {
			in = new FileInputStream(filename);
			cache.load(in);
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cache;
	}

	private void create(Properties properties, String filename)
			throws IOException {
		File f = new File(filename);
		if (f.exists()) {
			f.delete();
		}
		properties.store(new FileOutputStream(filename), null);
	}

	public static class UnformatedEndpoints {
		public static final String AuthEndpoint = "http://%s/RestServices/AuthServiceREST.svc";
		public static final String ClientEndpoint = "http://%s/RestServices/ClientServiceREST.svc";
		public static final String DocumentEndpoint = "http://%s/RestServices/DocumentServiceREST.svc";
		public static final String EmployeeEndpoint = "http://%s/RestServices/EmployeeServiceREST.svc";
		public static final String OrderEndpoint = "http://%s/RestServices/OrderServiceREST.svc";
		public static final String ParamEndpoint = "http://%s/RestServices/ParamServiceREST.svc";
		public static final String ProductEndpoint = "http://%s/RestServices/ProductServiceREST.svc";
		public static final String TaskEndpoint = "http://%s/RestServices/TaskServiceREST.svc";
	}

	public static Configuration AuthService() {
		Settings s = getSettings();
		if (s != null) {
			String url = String.format(UnformatedEndpoints.AuthEndpoint,
					s.getServerUrl());
			Configuration c = new Configuration(url, s.getToken());
			return c;
		}
		return null;
	}

	public static Configuration ClientService() {
		Settings s = getSettings();
		if (s != null) {
			String url = String.format(UnformatedEndpoints.ClientEndpoint,
					s.getServerUrl());
			Configuration c = new Configuration(url, s.getToken());
			return c;
		}
		return null;
	}

	public static Configuration DocumentService() {
		Settings s = getSettings();
		if (s != null) {
			String url = String.format(UnformatedEndpoints.DocumentEndpoint,
					s.getServerUrl());
			Configuration c = new Configuration(url, s.getToken());
			return c;
		}
		return null;
	}

	public static Configuration EmployeeService() {
		Settings s = getSettings();
		if (s != null) {
			String url = String.format(UnformatedEndpoints.EmployeeEndpoint,
					s.getServerUrl());
			Configuration c = new Configuration(url, s.getToken());
			return c;
		}
		return null;
	}

	public static Configuration OrderService() {
		Settings s = getSettings();
		if (s != null) {
			String url = String.format(UnformatedEndpoints.OrderEndpoint,
					s.getServerUrl());
			Configuration c = new Configuration(url, s.getToken());
			return c;
		}
		return null;
	}

	public static Configuration ParamService() {
		Settings s = getSettings();
		if (s != null) {
			String url = String.format(UnformatedEndpoints.ParamEndpoint,
					s.getServerUrl());
			Configuration c = new Configuration(url, s.getToken());
			return c;
		}
		return null;
	}

	public static Configuration ProductService() {
		Settings s = getSettings();
		if (s != null) {
			String url = String.format(UnformatedEndpoints.ProductEndpoint,
					s.getServerUrl());
			Configuration c = new Configuration(url, s.getToken());
			return c;
		}
		return null;
	}

	public static Configuration TaskService() {
		Settings s = getSettings();
		if (s != null) {
			String url = String.format(UnformatedEndpoints.TaskEndpoint,
					s.getServerUrl());
			Configuration c = new Configuration(url, s.getToken());
			return c;
		}
		return null;
	}
}
